package com.wiz.weixin.controllor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wiz.weixin.common.ResponseCode;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.util.PastUtil;

/**
 * 导航
 * @author xxy
 */
@Controller
@RequestMapping("/navigation")
@SuppressWarnings("all")
public class NavigationControllor implements ResponseCode{
	
	@Resource
	private TencentManager tencentManager;
	@Resource
	private StationService stationService;
	
	/**跳转到页面中，得到当前位置*/
	@RequestMapping(value="/toLocation")
	public String toLocation(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception {
		// 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
		request.setCharacterEncoding("UTF-8");  
		response.setCharacterEncoding("UTF-8"); 
		String openId = (String) session.getAttribute("openid");
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
			session.setAttribute("openid", openId);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "未找到微信关联用户");
			return "/error";
		}
		PastUtil.setJsSdkInModel(model);
		Map<String, String> map = new HashMap<String, String>();
		List<String> hqIds = stationService.getHQIds(openId);
		if(hqIds == null || hqIds.size()<= 0){
			model.addAttribute("msg", "对不起，您还未注册，请扫描加油站的二维码进行注册");
			return "/error";
		}
//		map.put("code", code);
//		model.addAttribute("map", map);	
		return "/navigation/navigation";
		
	}
	
	/**根据当前位置得到油站列表*/
	@RequestMapping(value="/stationList")
	@ResponseBody
	public String stationsByOpenid(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "lat", required = true) double lat,//经度
			@RequestParam(value = "lng", required = true) double lng,//纬度
			HttpSession session, Model model) throws Exception {
		 // 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8"); 
    	String openId = (String) session.getAttribute("openid");
    	if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "未找到微信关联用户");
			return "/error";
		}
    	/**得到当前用户所注册的所有总部*/
		List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
		if(hqIds == null || hqIds.size()<= 0){
			hqIds = stationService.getHQIds(openId);
			session.setAttribute("hqIds", hqIds);
		}
    	/**根据总部列表得到，附近的加油站*/
		return stationService.getNearbyStationsByHqIds(hqIds,lat,lng);// 30.635549,104.074860,
	}

}
